package com.ysc.kob.service.impl.user.bot;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysc.kob.entity.Bot;
import com.ysc.kob.entity.User;
import com.ysc.kob.mapper.BotMapper;
import com.ysc.kob.service.user.bot.BotService;
import com.ysc.kob.utils.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BotServiceImpl implements BotService {

    @Autowired
    private BotMapper botMapper;

    /**
     * 添加一个Bot
     * @param data
     * @return
     */
    @Override
    public Map<String, String> addBot(Map<String, String> data) {
        Map<String, String> map = Common.getMap();
        User user = Common.currentUser();
        String title = data.get("title");
        String description = data.get("description");
        String content = data.get("content");
        if (title == null || title.length() == 0) {
            map.put("error_message", "标题不能为空");
            return map;
        }

        if (title.length() > 100) {
            map.put("error_message", "标题长度不能大于100");
            return map;
        }

        if (description == null || description.length() == 0) {
            description = "这个用户很懒，什么也没留下~";
        }

        if (description.length() > 300) {
            map.put("error_message", "Bot描述的长度不能大于300");
            return map;
        }

        if (content == null || content.length() == 0) {
            map.put("error_message", "代码不能为空");
            return map;
        }

        if (content.length() > 10000) {
            map.put("error_message", "代码长度不能超过10000");
            return map;
        }

        Date now = new Date();
        Bot bot = new Bot(null, user.getId(), title, description, content, 1500, now, now);
        botMapper.insert(bot);
        map.put("error_message", "success");

        return map;
    }

    /**
     * 删除Bot
     * @param data
     * @return
     */
    @Override
    public Map<String, String> remove(Map<String, String> data) {
        User user = Common.currentUser();
        Map<String, String> map = Common.getMap();
        String botId = data.get("bot_id");
        int bot_id = Integer.parseInt(botId);

        Bot bot = botMapper.selectById(bot_id);
        if ( !bot.getUserId().equals(user.getId()) ) {
            map.put("error_message", "没有权限删除该Bot");
            return map;
        }

        if ( bot == null ) {
            map.put("error_message", "该bot不存在或已经被删除");
            return map;
        }

        botMapper.deleteById(bot_id);
        map.put("error_message", "success");
        return map;
    }

    /**
     * 更新Bot
     * @param data
     * @return
     */
    @Override
    public Map<String, String> update(Map<String, String> data) {
        User user = Common.currentUser();
        Map<String, String> map = Common.getMap();
        int bot_id = Integer.parseInt(data.get("bot_id"));

        String title = data.get("title");
        String description = data.get("description");
        String content = data.get("content");

        if (title == null || title.length() == 0) {
            map.put("error_message", "标题不能为空");
            return map;
        }

        if (title.length() > 100) {
            map.put("error_message", "标题长度不能大于100");
            return map;
        }

        if (description == null || description.length() == 0) {
            description = "这个用户很懒，什么也没留下~";
        }

        if (description.length() > 300) {
            map.put("error_message", "Bot描述的长度不能大于300");
            return map;
        }

        if (content == null || content.length() == 0) {
            map.put("error_message", "代码不能为空");
            return map;
        }

        if (content.length() > 10000) {
            map.put("error_message", "代码长度不能超过10000");
            return map;
        }

        Bot bot = botMapper.selectById(bot_id);

        if (bot == null) {
            map.put("error_message", "Bot不存在或已被删除");
            return map;
        }

        if (!bot.getUserId().equals(user.getId())) {
            map.put("error_message", "没有权限修改该Bot");
            return map;
        }

        Bot new_bot = new Bot(
                bot.getId(),
                user.getId(),
                title,
                description,
                content,
                bot.getRating(),
                bot.getCreateTime(),
                new Date()
        );

        botMapper.updateById(new_bot);

        map.put("error_message", "success");

        return map;
    }

    /**
     * 获取所有Bot
     * @return
     */
    @Override
    public List<Bot> getList() {
        User user = Common.currentUser();
        List<Bot> botList = botMapper.selectList(new LambdaQueryWrapper<Bot>()
                .eq(Bot::getUserId, user.getId()));
        return botList;
    }
}
